<template>
  <div>
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="60px" class="demo-dynamic">
      <span style="font-size:35px;font-weight:600;">首页业务领域</span><br>
      <el-form-item v-for="(domain, index) in dynamicValidateForm.domains" :label="'领域' + (index+1)" :key="domain.key"
        :prop="'domains.' + index + '.desc'">
        <el-input class="inputbox" type="textarea" v-model="domain.introduce"></el-input>
        <el-button type="primary" @click="change(domain)">确认</el-button>
        <el-button type="danger" @click.prevent="removeDomain(domain)">删除</el-button>
      </el-form-item>

      <!-- <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button> -->
      <el-button @click="addDomain">新增领域</el-button>

    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Service',
  data () {
    return {
      dynamicValidateForm: {
        domains: []
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.imgList)
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    removeDomain (item) {
      var index = this.dynamicValidateForm.domains.indexOf(item)
      this.$axios({
        method: 'get',
        url: '/area/delete?id=' + item.id
      }).then((res) => {
        if (res.status === 200) {
          this.open('删除成功！')
          if (index !== -1) {
            this.dynamicValidateForm.domains.splice(index, 1)
          }
        }
      })
    },
    addDomain () {
      this.dynamicValidateForm.domains.push({
        id: '',
        desc: ''
      })
    },
    open (text) {
      this.$notify({
        title: '成功',
        message: text,
        type: 'success'
      })
    },
    change (item) {
      if (item.id && item.introduce) {
        this.$axios({
          method: 'put',
          url: '/area/update',
          data: {
            'id': item.id,
            'introduce': item.introduce
          }
        }).then((res) => {
          if (res.status === 200) {
            this.open('修改成功！')
          }
        })
      } else if (item.id === '' && item.introduce) {
        this.$axios({
          method: 'post',
          url: '/area/insert',
          data: {
            'introduce': item.introduce
          }
        }).then((res) => {
          if (res.status === 200) {
            this.open('添加成功！')
          }
        })
      }
    },
    getService () {
      this.$axios({
        method: 'get',
        url: '/area/select'
      }).then((res) => {
        this.dynamicValidateForm.domains = res.data.data
      })
    }
  },
  mounted () {
    this.getService()
  }
}
</script>

<style scoped>
.imgItem {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  overflow: hidden;
}
.img {
  width: 450px;
  height: 300px;
}
.el-form-item {
  margin: 20px 20px;
  font-size: 25px;
  font-weight: 600;
}
.el-button {
  margin: 15px 0;
}
</style>
